<template>
  <div class="order">
    <div class="search">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="客户姓名">
          <el-input v-model="formData.name" placeholder="请输入客户姓名" />
        </el-form-item>
        <el-form-item label="客户电话">
          <el-input v-model="formData.phone" placeholder="请输入客户电话" />
        </el-form-item>
        <el-form-item label="订单编号">
          <el-input v-model="formData.ordercode" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="负责人">
          <el-input v-model="formData.username" placeholder="请输入负责人姓名" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getOrders">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="dialogVisible = true">添加</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <el-table :data="orders" border>
        <el-table-column label="客户姓名" prop="object.name" />
        <el-table-column label="客户电话" prop="object.phone" />
        <el-table-column label="订单编号" prop="ordercode" />
        <el-table-column label="订单详情" prop="details">
          <template slot-scope="scope">
            <el-popover
              placement="right"
              width="400"
              trigger="click"
            >
              <el-table :data="scope.row.details">
                <el-table-column property="product.name" label="产品名" />
                <el-table-column property="product.brand" label="品牌" />
                <el-table-column property="product.model.info" label="产品类型" />
                <el-table-column property="quantity" label="数量" />
              </el-table>
              <el-button slot="reference">详情</el-button>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="销售负责人" prop="account.username" />
        <el-table-column label="订单状态" prop="status" />
        <el-table-column label="销售总价" prop="price" />
        <el-table-column label="创建时间" prop="createTime" />
        <el-table-column label="操作" fixed="right">
          <template slot-scope="scope">
            <el-button size="small" type="danger" @click="remove(scope.row)">删除</el-button>
            <el-button size="small" type="danger" @click="update(scope.row)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination
        :page-size="limit"
        :total="total"
        style="padding:30px 0;text-align:center;"
        layout="total,prev,pager,next,jumper"
        @current-change="getPagedata"
      />
    </div>
    <el-dialog
      title="添加"
      :visible.sync="dialogVisible"
      width="60%"
      :before-close="handleupdateClose"
    >
      <el-form>
        <el-form-item>
          <el-select v-model="orderInfo.whoId" placeholder="请选择客户">
            <el-option
              v-for="custmer in custmers"
              :key="custmer.id"
              :label="custmer.name"
              :value="custmer.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="orderInfo.accountId" placeholder="请选择负责人">
            <el-option
              v-for="account in accounts"
              :key="account.id"
              :label="account.username"
              :value="account.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <el-table :data="stockProducts" border :selectable="true" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="商品名称" prop="product.name"></el-table-column>
        <el-table-column label="商品品牌" prop="product.brand"></el-table-column>
        <el-table-column label="库存余量" prop="quantity"></el-table-column>
        <el-table-column label="销售单价" prop="sellingPrice"></el-table-column>
        <el-table-column label="出库数量" prop="number">
          <template #default="{row}">
            <el-input v-model="row.number" type="number" clearable />
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrder">确定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="修改"
      :visible.sync="updateVisible"
      width="50%"
      :before-close="handleupdateClose"
    >
      <el-form ref="updateorderInfo" :model="updateorderInfo" label-position="right">
        <el-form-item label="客  户:" prop="model">
          <el-select v-model="updateorderInfo.name" placeholder="请选择客户" style="width: 400px">
            <el-option
              v-for="item in names"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="订单编号:" prop="ordercode">
          <el-input v-model="updateorderInfo.ordercode" :disabled="true" style="width: 400px" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleupdate('updateorderInfo')">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import orderApi from '@/api/order/order.js'
import customerApi from '@/api/customer/customer.js'
import accountApi from '@/api/account/account.js'
export default {
  name: 'Order',
  data() {
    return {
      formData: {},
      current: 1, // 当前页
      limit: 10, // 每页显示记录数
      total: 0, // 总记录数
      updateVisible: false,
      dialogVisible: false,
      accounts: [],
      custmers: [],
      orders: [],
      stockProducts: [],
      orderInfo: {
        whoId: '',
        accountId: '',
        products: []
      },
      updateorderInfo: {
        ordercode: '',
        name: ''
      },
      names: null
    }
  },
  created() {
    this.getOrders()
    this.getNames()
    this.getStockProducts()
    this.selectAllCustomer()
    this.selectAllAccounts()
  },
  methods: {
    selectAllAccounts() {
      accountApi.selectAllAccounts({}).then(res => {
        this.accounts = res.data.list
        console.log(res.data)
      })
    },
    selectAllCustomer() {
      customerApi.selectAllCustmer({}).then(res => {
        this.custmers = res.data.list
      })
    },
    handleSelectionChange(selection) {
      // selection 是一个数组，包含当前选中的所有行
      this.orderInfo.products = selection
      console.log(this.orderInfo)
    },
    addOrder() {
      this.dialogVisible = false
      orderApi.addOrder(this.orderInfo).then(res => {
        console.log(res)
        this.getOrders()
        this.getPagedata()
      })
    },
    getStockProducts() {
      orderApi.showStockProducts({}).then(res => {
        this.stockProducts = res.data.list
      })
    },
    getOrders() {
      this.formData.page = this.current
      this.formData.limit = this.limit
      this.formData.type = 1
      orderApi.showsell(this.formData).then(res => {
        this.orders.length = 0
        this.orders = res.data.list.map(order => {
          if (order.status === 1) {
            order.status = '待处理'
          } else if (order.status === 0) {
            order.status = '已处理'
          }
          return order
        })
        this.total = res.data.total
      })
    },
    // 分页获取
    getPagedata(page = 1) {
      this.current = page
      this.formData.page = this.current
      this.formData.limit = this.limit
      orderApi.showsell(this.formData).then(
        res => {
          this.orders = res.data.list
          this.total = res.data.total
        }
      )
    },
    remove(row) {
      this.$confirm('此操作将永久删除,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => { // 确定执行then方法
        // 调用接口
        // 删除
        console.log(row)
      })
    },
    update(row) {
      this.updateVisible = true
      this.updateorderInfo.ordercode = row.ordercode
      this.updateorderInfo.name = row.name
      this.updateorderInfo.status = row.status
      console.log(this.updateorderInfo)
    },
    getNames() {
      customerApi.showName().then(res => {
        this.names = res.data
      })
    },
    handleupdateClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.$message('取消操作')
          done()
        })
        .catch(_ => {})
    },
    handleupdate(updateorderInfo) {
      this.updateVisible = false
      this.$refs[updateorderInfo].validate((valid) => {
        if (valid) {
          orderApi.updateSell(this.updateorderInfo).then(res => {
            console.log(res)
            this.getOrders()
          })
          console.log(this.updateorderInfo)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">

</style>
